from fastapi import WebSocket, APIRouter


websocket_router = APIRouter()


@websocket_router.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket):
    await websocket.accept()
    while True:
        # 接收客户端发送的消息
        data = await websocket.receive_text()
        print(f"Received message: {data}")
        # 发送消息给客户端
        response = f"Message text was: {data}"
        await websocket.send_text(response)